package com.admin.system.controller;

import com.admin.base.BaseController;
import com.admin.base.Response;
import com.alibaba.fastjson.JSON;
import com.admin.system.entity.SystemResource;
import com.admin.system.entity.SystemRole;
import com.admin.system.service.SystemResourceService;
import com.admin.system.service.SystemRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* @author : alex
* @date : 2020/5/23
*/
@Controller
@RequestMapping(value = "/system/resource")
public class SystemResourceController extends BaseController {

  @Autowired
  SystemResourceService resourceService;
  @Autowired
  SystemRoleService roleService;

  /**
   * 列表页面
   */
  @RequestMapping(value = "/list")
  public ModelAndView list(HttpServletRequest request, HttpServletResponse response, ModelAndView mav) {
    mav.setViewName("system/systemResource/list");
    return mav;
  }

  @RequestMapping(value = "/tree")
  @ResponseBody
  public Response resourceTree(HttpServletRequest request, HttpServletResponse response, ModelAndView mav) {
    return Response.ok();
  }

  @RequestMapping(value = "/asyncTree")
  @ResponseBody
  public List<SystemResource> asyncTree(HttpServletRequest request, HttpServletResponse response, ModelAndView mav, Integer id) {
    // 利用父节点ID加载子节点
    List<SystemResource> result = resourceService.getResourceByPid(id);
    return result;
  }

  /**
   * 保存提交的菜单信息
   * @param request
   * @param response
   * @param resource
   * @return
   */
  @RequestMapping(value = "/save")
  @ResponseBody
  public Response save(HttpServletRequest request, HttpServletResponse response, SystemResource resource) {
    resourceService.save(resource);
    return Response.ok();
  }

  @RequestMapping(value = "/delete")
  @ResponseBody
  public Response delete(HttpServletRequest request, HttpServletResponse response, SystemResource resource) {
    resourceService.delete(resource);
    return Response.ok();
  }

  /**
   * 菜单复选页面
   */
  @RequestMapping(value = "/toAssign")
  public ModelAndView check(HttpServletRequest request, HttpServletResponse response, ModelAndView mav, Integer roleId) {
    mav.setViewName("system/systemRole/assign");
    Object zNodes = JSON.toJSON(resourceService.assignedResources(roleId));
    SystemRole role = roleService.queryById(roleId);
    mav.addObject("zNodes", zNodes);
    mav.addObject("role", role);
    return mav;
  }

  @RequestMapping(value = "/assign")
  @ResponseBody
  public Response assign(HttpServletRequest request, HttpServletResponse response, Integer roleId, Integer[] ids) {
    // 新提交数据
    List<Integer> submission = new ArrayList<>();
    if (ids != null) {
      submission = Arrays.asList(ids);
    }
    resourceService.assignResources(roleId, submission);
    return Response.ok();
  }

}